package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.exception.BusinessException;
import com.demo.mapper.FriendshipMapper;
import com.demo.mapper.UserMapper;
import com.demo.pojo.Friendship;
import com.demo.pojo.FriendshipVO;
import com.demo.pojo.ResultVO;
import com.demo.pojo.User;
import com.demo.service.FriendshipService;
import com.demo.utils.CurrentHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class FriendshipServiceImpl extends ServiceImpl<FriendshipMapper, Friendship> implements FriendshipService {
    @Autowired
    private UserMapper  userMapper;

    /**
     * 添加好友
     * @param friendship
     * @return
     */
    @Override
    public ResultVO add(Friendship friendship) {
        friendship.setUserId(CurrentHolder.getCurrentId());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",friendship.getFriendUsername());
        User user=userMapper.selectOne(queryWrapper);
        if(user==null){
            throw new BusinessException("您用户不存在");
        }
        friendship.setFriendId(user.getId());
        if(friendship.getRemark()== null||friendship.getRemark().isEmpty()){
            throw new BusinessException("没有输入好友备注");
        }
        friendship.setStatus(0);
        friendship.setCreateTime(LocalDateTime.now());
        boolean save = this.save(friendship);
        if(!save){
            throw new BusinessException("添加好友失败");
        }
        return ResultVO.success();
    }

    /**
     * 删除好友
     * @param friendship
     * @return
     */
    @Override
    public ResultVO delete(Friendship friendship) {
        if(friendship.getFriendUsername()==null||friendship.getFriendUsername().isEmpty()){
            throw new BusinessException("没有输入要删除的好友用户名");
        }
        friendship.setUserId(CurrentHolder.getCurrentId());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",friendship.getFriendUsername());
        User user = userMapper.selectOne(queryWrapper);
        if(user==null){
            throw new BusinessException("没有该用户");
        }
        friendship.setFriendId(user.getId());
        QueryWrapper<Friendship> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id",friendship.getUserId());
        queryWrapper1.eq("friend_id",friendship.getFriendId());
        boolean remove = this.remove(queryWrapper1);
        if(!remove){
            throw new BusinessException("删除失败");
        }


        return ResultVO.success();
    }

    /**
     * 获取当前用户的所有好友
     * @return
     */
    @Override
    public ResultVO<List<FriendshipVO>> getAlList() {
        Long currentId = CurrentHolder.getCurrentId();
        List<Friendship> friendships = this.list(new QueryWrapper<Friendship>().eq("user_id", currentId));
        if(friendships.size() > 0){
            List<FriendshipVO> list = new ArrayList<>();
            for (Friendship friendship : friendships) {
                FriendshipVO friendshipVO = new FriendshipVO();
                User user = userMapper.selectById(friendship.getFriendId());
                friendshipVO.setId(friendship.getFriendId());
                friendshipVO.setUsername(user.getUsername());
                friendshipVO.setEmail(user.getEmail());
                friendshipVO.setRemark(friendship.getRemark());
                list.add(friendshipVO);
            }
            return ResultVO.success(list);

        }
        return ResultVO.success(null);
    }
}
